home *** CD-ROM | disk | FTP | other *** search
- Path: nwlink.com!usenet
- From: Teresa Reiko <tjr19@mail.nwlink.com>
- Newsgroups: comp.lang.c
- Subject: Re: Permutations
- Date: 28 Feb 1996 16:15:03 GMT
- Organization: Northwest Link
- Message-ID: <4h1v27$cdc@texas.nwlink.com>
- References: <Dn8yz9.GGy@spuddy.mew.co.uk>
- NNTP-Posting-Host: port31.annex3.nwlink.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.2 (Windows; U; 16bit)
-
- david@spuddy.mew.co.uk (David Turner) wrote:
-
- > Does anyone know of an algorithm that works out the different permutations
- >of ordering a number of objects? Not calculating how many there are, but
- >actaully working out (quickly?) what they permutations are!
-
- Untested code -- but this should work...
- Example: Permute("", "abcdef", 6)
-
- void Permute(char *p_prefix, char *p_list, int num)
- {
- int i;
- char s[10], p[10];
-
- if(num == 0)
- {
- printf("%s%s", prefix, p_list);
- return;
- }
-
- for(i = 0; i < num; i++)
- {
- s[1] = 0;
- s[0] = p_list[i];
- strcpy(p, p_prefix);
- strcat(p, s);
-
- strncpy(s, p_list, i);
- s[i] = 0;
- strcat(s, p_list + i + 1);
-
- Permute(p, s, num - 1);
- }
- }
-
- ----------------------------------------------------------------------
- Teresa Reiko Chief Programmer, Tenbyte Software tjr19@nwlink.com
- ----------------------------------------------------------------------
-
-
-